package com.lineage.data.event;

import com.lineage.config.ConfigOther;
import com.lineage.data.executor.EventExecutor;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Event;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.world.World;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class OnlineGiftSet extends EventExecutor {
  private static final Log _log = LogFactory.getLog(OnlineGiftSet.class);
  
  private static int _time = 0;
  
  private static final Map<Integer, ArrayList<GetItemData>> _giftList = new HashMap<>();
  
  private static final Map<L1PcInstance, Integer> _getMap = new ConcurrentHashMap<>();
  
  public static EventExecutor get() {
    return new OnlineGiftSet();
  }
  
  public static void add(L1PcInstance tgpc) {
    if (_time == 0)
      return; 
    if (_getMap.get(tgpc) == null)
      _getMap.put(tgpc, Integer.valueOf(_time)); 
  }
  
  public static void remove(L1PcInstance tgpc) {
    if (_time == 0)
      return; 
    _getMap.remove(tgpc);
  }
  
  private static void getitem(L1PcInstance tgpc) {
    try {
      if (check(tgpc)) {
        if (tgpc.getLevel() < ConfigOther.giftset)
          return; 
        ArrayList<GetItemData> value = null;
        if (tgpc.isPrivateShop()) {
          value = _giftList.get(new Integer(1));
        } else if (tgpc.isFishing()) {
          value = _giftList.get(new Integer(2));
        } else if (tgpc.getMapId() != 5300) {
          value = _giftList.get(new Integer(3));
        } 
        if (value == null)
          return; 
        Iterator<GetItemData> iterator = value.iterator();
        while (iterator.hasNext()) {
          GetItemData iteminfo = iterator.next();
          if (iteminfo == null)
            continue; 
          L1ItemInstance item = ItemTable.get().createItem(iteminfo._getItemId);
          item.setCount(iteminfo._getAmount);
          if (item == null || tgpc.getInventory().checkAddItem(item, 1L) != 0)
            continue; 
          tgpc.getInventory().storeItem(item);
          tgpc.sendPackets((ServerBasePacket)new S_ServerMessage("获得连线奖励：" + item.getLogName()));
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public static boolean check(L1PcInstance tgpc) {
    try {
      if (tgpc == null)
        return false; 
      if (tgpc.getOnlineStatus() == 0)
        return false; 
      if (tgpc.getNetConnection() == null)
        return false; 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return false;
    } 
    return true;
  }
  
  public void execute(L1Event event) {
    PerformanceTimer timer = new PerformanceTimer();
    boolean isError = false;
    try {
      String[] set = event.get_eventother().split(",");
      try {
        _time = Integer.parseInt(set[0]);
        if (_time <= 0) {
          _log.error("设定给予奖励的时间(分钟)异常 - 将不启用本项设置");
          isError = true;
          return;
        } 
      } catch (Exception e2) {
        _log.error("设定给予奖励的时间(分钟)异常 - 将不启用本项设置");
        isError = true;
        return;
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return;
    } finally {
      if (!isError) {
        GetItemTimer getItemTimer = new GetItemTimer();
        getItemTimer.start();
        _log.info("载入给予奖励物品: " + _giftList.size() + "(" + timer.get() + "ms)");
      } else {
        _time = 0;
        _giftList.clear();
      } 
    } 
    if (!isError) {
      GetItemTimer getItemTimer = new GetItemTimer();
      getItemTimer.start();
      _log.info("载入给予奖励物品: " + _giftList.size() + "(" + timer.get() + "ms)");
    } else {
      _time = 0;
      _giftList.clear();
    } 
  }
  
  private class GetItemData {
    private GetItemData() {}
    
    public int _getItemId = 40308;
    
    public int _getAmount = 1;
  }
  
  private class GetItemTimer extends TimerTask {
    private ScheduledFuture<?> _timer;
    
    private GetItemTimer() {}
    
    public void start() {
      int timeMillis = 60000;
      this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 60000L, 60000L);
    }
    
    public void run() {
      try {
        if (!OnlineGiftSet._getMap.isEmpty()) {
          Iterator<L1PcInstance> iterator = OnlineGiftSet._getMap.keySet().iterator();
          while (iterator.hasNext()) {
            L1PcInstance tgpc = iterator.next();
            Thread.sleep(1L);
            if (World.get().getPlayer(tgpc.getName()) == null) {
              OnlineGiftSet._getMap.remove(tgpc);
              continue;
            } 
            Integer time = (Integer)OnlineGiftSet._getMap.get(tgpc);
            if (time == null)
              continue; 
            time = Integer.valueOf(time.intValue() - 1);
            if (time.intValue() <= 0) {
              OnlineGiftSet.getitem(tgpc);
              OnlineGiftSet._getMap.put(tgpc, Integer.valueOf(OnlineGiftSet._time));
              continue;
            } 
            OnlineGiftSet._getMap.put(tgpc, time);
          } 
        } 
      } catch (Exception e) {
        OnlineGiftSet._log.error("在线奖励清单时间轴异常重启", e);
        GeneralThreadPool.get().cancel(this._timer, false);
        GetItemTimer getItemTimer = new GetItemTimer();
        getItemTimer.start();
      } 
    }
  }
}
